<?php
/**
 * Created by PhpStorm.
 * User: lance
 * Date: 11/28/17
 * Time: 4:15 PM
 */

/**
 * 客户存款管理
 */

namespace app\customer\admin;


use app\admin\controller\Admin;
use app\common\builder\ZBuilder;
use think\Db;

class Deposit extends Admin
{
    function _list()
    {
        $a_where = [];
        //客户经历只能看自己的客户
        defined('MANAGER_ID') && ($a_where['b.manager_id'] = MANAGER_ID);
        $a_where += $this->getMap();
        $a_dataList = Db::name('customer_deposit a')
            ->field(['a.id', 'b.name customer_name', 'a.number', 'a.money', 'a.time', 'a.deposit_time'])
            ->join('customer b', 'b.id=a.customer_id')
            ->where($a_where)
            ->order('id desc')->paginate();
        return ZBuilder::make('table')
            ->hideCheckbox()
            ->addColumns([
                ['id', 'ID'],
                ['customer_name', '客户'],
                ['number', '存款单号', 'text.edit'],
                ['money', '存款金额', 'number'],
                ['time', '存款时间', 'datetime.edit'],
                ['deposit_time', '存款时长/月', 'number']
            ])
            ->addValidate('Deposit', 'number,money,deposit_time')
            ->setRowList($a_dataList)
            ->fetch();
    }

    public function add()
    {
        if (request()->isAjax()):
            //数据校验
            $o_validate = validate('Deposit');
            if ($o_validate->check(input('')) === false):
                $this->error($o_validate->getError());
            endif;
            //添加数据
            $o_model = model('CustomerDeposit');
            if (!$o_model->save(input(''))):
                $this->error("操作失败");
            endif;
            $this->success("操作成功");

        else:
            return ZBuilder::make('form')
                ->addHidden('customer_id', input('customer_id'))
                ->addFormItems([
                    ['text', 'number', '存款单号', '必填'],
                    ['number', 'money', '存款金额/元', '必须大于0', 1],
                    ['datetime', 'time', '存款时间', '必填', date('Y-m-d H:i:s')],
                    ['number', 'deposit_time', '存款时长/月', '必须大于0', 1]
                ])
                ->fetch();
        endif;
    }
}